from db.sql import engine
from db.sql import Point
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import InvalidRequestError
from  sqlalchemy.sql.expression import func, select
import os

DBSession = sessionmaker(bind=engine)
session = DBSession()


def insert(object):
    try:
        if type(object) == list:
            for i in object:
                session.add(i)
        else:
            session.add(object)
        session.commit()
        return True
    except InvalidRequestError:
        session.rollback()
    except Exception as e:
        print(e)
        return False


def query_by_edgeId(edge_id):
    return session.query(Point).filter(Point.edge_id == edge_id).all()